import { request, GET, POST } from '../utils/request';
import { getCookies } from '../utils/cookie'

export function getStatus() {
  const { userid, token } = getCookies();
  if (!token) return;
  const headers = {
    'Authorization': `Bearer ${token}`
  }
  return GET(`/api/v1/status/${userid}`, headers);
}

export function sendMessage(message) {
  const { title, content } = message;
  const { userid, group, token } = getCookies();
  if (!token) return;
  const headers = {
    'Authorization': `Bearer ${token}`
  }
  const messageTs = new Date().getTime();
  const messageId = messageTs + userid;
  const messageType = "send";
  let url = '/api/v1/messages/new?'
        +`userid=${userid}&`
        +`messageId=${messageId}&`
        +`messageTs=${messageTs}&`
        +`messageType=${messageType}&`
        +`title=${title}&`
        +`content=${content}&`
        +`group=${group}&`
        +`replyId=${''}`;

  return GET(url, headers);
}

export function replyMessage(message) {
  const { title, content, messageId } = message;
  const { userid, group, token } = getCookies();
  if (!token) return;
  const headers = {
    'Authorization': `Bearer ${token}`
  }
  const messageTs = new Date().getTime();
  const replyMessageId = userid + messageTs;
  const messageType = "reply";
  let url = '/api/v1/messages/new?'
        +`userid=${userid}&`
        +`messageId=${replyMessageId}&`
        +`messageTs=${messageTs}&`
        +`messageType=${messageType}&`
        +`title=${title}&`
        +`content=${content}&`
        +`group=${group}&`
        +`replyId=${messageId}`;

  return GET(url, headers);
}

export function getMessageByGroup(group) {
  const { token } = getCookies();
  if (!token) return;
  const headers = {
    'Authorization': `Bearer ${token}`
  }
  let url = '/api/v1/messages?'
        +`group=${group}&`
        +'type=group';

  return GET(url, headers);
}

export function getMessageByUserid(userid) {
  const { token } = getCookies();
  if (!token) return;
  const headers = {
    'Authorization': `Bearer ${token}`
  }
  let url = '/api/v1/messages?'
        +`userid=${userid}&`
        +'type=userid';

  return GET(url, headers);
}

export function getMessageByReply(userid) {
  const { token } = getCookies();
  if (!token) return;
  const headers = {
    'Authorization': `Bearer ${token}`
  }
  let url = '/api/v1/messages?'
        +`userid=${userid}&`
        +'type=reply';

  return GET(url, headers);
}

